---
title: 'Replication file: Negative Political Identities and Costly Political Action'
author: "Katharina Lawall, Stuart J. Turnbull-Dugarte, Florian Foos \& Josh Townsley"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  pdf_document: default
  bookdown::pdf_document2:
    toc: false
    keep_tex: yes
    latex_engine: xelatex
abstract:
  
keywords: 
mainfont: cochineal
fontsize: 11pt
---


<!-- Set your working directory here -->
```{r setup, include=FALSE, cache = FALSE}
#install.packages("knitr")
rm(list=ls())
require("knitr")
#knitr::opts_knit$set(root.dir = "C:/Users/katharina.lawall/Downloads/NegativePoliticalIdentities")

```


<!--Load all the required libraries in -->

```{r libraries, include=FALSE, cache = FALSE}

library(margins)
library(ri2)
library(estimatr)
library(texreg)
library(dplyr)
library(tidyr)

library(ggplot2)
library(stringr)

library(randomizr)
library(estimatr)


library(purrr)

library(ggpubr)


library(stargazer)

library(jtools)
```

<!--Loading in  data -->

```{r dataload, , echo=FALSE}


data <- read.csv("data/fieldexpdata.csv") 

surveydata <- read.csv("data/surveydata.csv")
```







# Set up character values for decision to donate outcomes

```{r recodeoutcomes, echo=FALSE}



donor <- "donor7days"
names(donor) <- "Donated (=1), did not donate (=0), 7 days post-treatment"
 attr(donor,'shortname') <- "donations (binary)"
 
donor_2days <- "donor2days"
names(donor_2days) <- "Donated (=1), did not donate (=0)"
 attr(donor,'shortname') <- "donations (binary)"
 
donor_14days <- "donor14days"
names(donor_14days) <- "Donated (=1), did not donate (=0), 14 days post-treatment"





``` 
# Set up character values for number of times donated outcomes

```{r recodeoutcomes2, echo=FALSE}


data <- data %>% mutate (log_donation_nr7days = log(donation_nr7days+1)) #create log transformed variable for robustness 




donation_nr <- "donation_nr7days"
names(donation_nr) <- "How many times did an individual donate? 7 days post-treatment"
attr(donation_nr, 'shortname') <- "donations (nr of times)"

donation_nr_2days <- "donation_nr2days"
names(donation_nr_2days) <- "How many times did an individual donate?"

donation_nr_14days <- "donation_nr14days"
names(donation_nr_14days) <- "How many times did an individual donate? 14 days post-treatment"

log_donation_nr7days <- "log_donation_nr7days"
names(log_donation_nr7days) <- "Logged: How many times did an individual donate? 7 days post-treatment"

```

# Set up for total amount donated outcomes

```{r recodeoutcomes3, echo=FALSE}


donation_amount <- "donation_total7days"
names(donation_amount) <- "How much did an individual donate? (in GBP) 7 days post-treatment"
attr(donation_amount, 'shortname') <- "donations (amount)"



donation_amount_14days <- "donation_total14days"
names(donation_amount_14days) <- "How much did an individual donate? (GBP) 14 days post-treatment"
donation_amount_2days <- "donation_total2days"


# log transform total amount donated
data <- data %>% mutate (log_donation_total7days = log(donation_total7days+1),
                         log_donation_total2days = log(donation_total2days+1),
                         log_donation_total14days = log(donation_total14days+1))

log_donation_amount_7days <- "log_donation_total7days"
names(log_donation_amount_7days) <- "Logged DV: How much did an individual donate? (in GBP) 7 days post-treatment"
attr(log_donation_amount_7days, 'shortname') <- "log donations (amount)"

log_donation_amount_14days <- "log_donation_total14days"
names(log_donation_amount_14days) <- "Logged DV: How much did an individual donate? (in GBP) 14 days post-treatment"
attr(log_donation_amount_14days, 'shortname') <- "log donations (amount)"

log_donation_amount_2days <- "log_donation_total2days"
names(log_donation_amount_2days) <- "Logged DV: How much did an individual donate? (in GBP)"


```





# Covariates



```{r covars, echo=F}

# create pre-treatment donor type variable

data <- data %>% mutate(donor.type = case_when(pret_donation_total1year==0 ~ "none",
                                               pret_donation_total1year>0 & pret_donation_total1year<=25 ~ "small", #25 is first quartile
                       pret_donation_total1year >25 ~ "large"),
                        donor.type = factor(donor.type, levels = c("none", "small", "large")))




```












# SI: Figure A.2, negative vs positive cue, outcome: number of donations

```{r figa2i, echo=F}

# panel i) : mean values and CI



#mean values and CI
summary_good <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(donation_nr7days ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))



g1 <- 
ggplot(data, aes(x=treatment_cat, y=predicted, color=treatment_cat)) +
  theme_minimal() +
  geom_point(data = summary_good, size = 2) +
  geom_errorbar(data = summary_good,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summary_good, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Number of donations (7 days post-treatment)" , title="i) Mean number of donations by experimental condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0.01,0.03)  +
  scale_color_manual(values=c('#999999','#2a94cf','#21719e', "#f1c232", "#e69138"))+
  scale_size_manual(values=c(2,3,4)) +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  geom_text(aes(x = 1.5,  y = 0.03, label = "\u03b2=0.003*")) +
  geom_segment(aes(x = 1, xend = 1, 
           y = 0.028, yend = 0.029),
           colour = "black") +
  geom_segment(aes(x = 2, xend = 2, 
           y = 0.028, yend = 0.029),
           colour = "black") +
  geom_segment(aes(x = 1, xend = 2, 
           y = 0.029, yend = 0.029),
           colour = "black")

g1


```

```{r figa2ii, echo=F}




modelX<-lm_robust (donation_nr7days ~ treat_negative, data=data)

gg_df <-
  modelX %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="7 days")

modelX_2days<-lm_robust (donation_nr2days ~ treat_negative, data=data)

gg_df_2days <-
  modelX_2days %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="2 days")

modelx_14days <- lm_robust(donation_nr14days ~ treat_negative, data=data)

gg_df_14days <-
  modelx_14days %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="14 days")

gg_df1 <- full_join(gg_df, gg_df_2days) %>% full_join(., gg_df_14days) %>%
  mutate(days_f = factor(days, levels = c("2 days", "7 days", "14 days")))

g2<- ggplot(gg_df1, aes(x=days_f, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = "Days since treatment email", y = NULL, title="ii) Marginal effect of negative vs positive cue \non  number of donations \n") +
    ylim(-0.005,0.009)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") 

g2


```


```{r figa2iii, echo=F}



# panel ii) marginal effects plot regression model




modelX<-lm_robust(donation_nr7days ~ treat_negative + treat_issue + treat_negative*treat_issue, data=data)
summary(modelX)
gg_df <-
  modelX %>%
  margins(at = list(treat_issue = 0:1)) %>%
  summary %>%
  as.data.frame() 

gg_df1 <- gg_df %>% filter(factor=="treat_negative") %>%
  mutate(treat_issuecat = case_when(treat_issue==0 ~ "Party cue",
                                    treat_issue==1 ~ "Issue cue"))


g3<- ggplot(gg_df1, aes(x=treat_issuecat, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = NULL, title="iii) Marginal effect of negative vs positive cue \non number of donations 7 days post-treatment \nconditional on issue identity cue", caption= "p<0.01=***, p<0.05=**, p<0.1=*") +
   ylim(-0.005,0.012)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") +
  geom_text(aes(x = 1.5,  y = 0.011, label = "\u03b2=0.005**")) +
  geom_segment(aes(x = 1, xend = 1, 
           y = 0.009, yend = 0.01),
           colour = "black") +
  geom_segment(aes(x = 2, xend = 2, 
           y = 0.009, yend = 0.01),
           colour = "black") +
  geom_segment(aes(x = 1, xend = 2, 
           y = 0.01, yend = 0.01),
           colour = "black")

g3 


p2 <- ggarrange(g2, g3, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")
p3 <- ggarrange(g1, p2, ncol=1, nrow=2, widths = c(1.1, 1), align="hv")

p3


ggsave("output/si_figa2_combined_nrdonations.png",width =24, height = 24, units = "cm", dpi=320)
```


# SI, Figure A.3, negative vs positive cue, outcome: logged donation amount



```{r figa3i, echo=F}

#mean values and CI
summary_good <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(log_donation_total7days ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))





g1 <- 
ggplot(data, aes(x=treatment_cat, y=predicted, color=treatment_cat)) +
  theme_minimal()+
  geom_point(data = summary_good, size = 2) +
  geom_errorbar(data = summary_good,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summary_good, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Log donation amount (7 days post-treatment)" , title="i) Mean donation amount (logged) by experimental condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0,0.1)  +
  scale_color_manual(values=c('#999999','#2a94cf','#21719e', "#f1c232", "#e69138"))+
  scale_size_manual(values=c(2,3,4)) +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) 
g1


```

```{r figa3ii, echo=F}


modelX<-lm_robust (log_donation_total7days ~ treat_negative, data=data)

gg_df <-
  modelX %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="7 days")

modelX_2days<-lm_robust (log_donation_total2days ~ treat_negative, data=data)

gg_df_2days <-
  modelX_2days %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="2 days")

modelx_14days <- lm_robust(log_donation_total14days ~ treat_negative, data=data)

gg_df_14days <-
  modelx_14days %>%
  margins() %>%
  summary %>%
  as.data.frame() %>%
  mutate(days="14 days")

gg_df1 <- full_join(gg_df, gg_df_2days) %>% full_join(., gg_df_14days) %>%
  mutate(days_f = factor(days, levels = c("2 days", "7 days", "14 days")))

g2<- ggplot(gg_df1, aes(x=days_f, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = "Days since treatment email", y = NULL, title="ii) Marginal effect of negative vs positive cue \non  logged donation amount \n") +
    ylim(-0.025,0.025)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") 

g2


```

```{r figa3iii, echo=F}



# panel ii) marginal effects plot regression model

modelX<-lm_robust(log_donation_total7days ~ treat_negative + treat_issue + treat_negative*treat_issue, data=data)
summary(modelX)
gg_df <-
  modelX %>%
  margins(at = list(treat_issue = 0:1)) %>%
  summary %>%
  as.data.frame() 

gg_df1 <- gg_df %>% filter(factor=="treat_negative") %>%
  mutate(treat_issuecat = case_when(treat_issue==0 ~ "Party cue",
                                    treat_issue==1 ~ "Issue cue"))


g3<- ggplot(gg_df1, aes(x=treat_issuecat, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = NULL, title="iii) Marginal effect of negative vs positive cue \non logged donation amount  7 days post-treatment \nconditional on issue identity cue", caption= "p<0.01=***, p<0.05=**, p<0.1=*") +
   ylim(-0.02,0.034)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") +
  geom_text(aes(x = 1.5,  y = 0.032, label = "\u03b2=0.019***")) +
  geom_segment(aes(x = 1, xend = 1, 
           y = 0.028, yend = 0.03),
           colour = "black") +
  geom_segment(aes(x = 2, xend = 2, 
           y = 0.028, yend = 0.03),
           colour = "black") +
  geom_segment(aes(x = 1, xend = 2, 
           y = 0.03, yend = 0.03),
           colour = "black")
g3


p2 <- ggarrange(g2, g3, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")
p3 <- ggarrange(g1, p2, ncol=1, nrow=2, widths = c(1.1, 1), align="hv")

p3

ggsave("output/si_figa3_combined_logdonamount.png",width =25, height = 25, units = "cm", dpi=320)
```





# SI, Table A.2: Balance table

```{r balance-table, echo=F}

data <- data %>% mutate(value1=1) %>% pivot_wider(names_from =dem_region,values_from=value1, values_fill = 0, names_prefix="region_") # create dummy vars for region


my_summarise <- function(data, group_var, summarise_var) {
      data %>%
    group_by(across({{ group_var }})) %>% 
    summarise(across({{ summarise_var }}, ~ paste(round(mean(.), digits = 2), " (", round(sd(.),digits = 1), ")", sep=""), .names = "{.col}")) #get mean and SD for each variable

}






my_chisq <- function(data, group_var, summarise_var) {
data %>%
  summarise(across({{ summarise_var }}, ~ chisq.test({{group_var}},.)$p.value))
}

demslist <- data %>% select(starts_with(c("dem", "region", "pret_donor1year"))) %>% colnames()




  
pvalue <- my_chisq(data, treatment_cat, demslist)

pvalue <- pvalue %>% mutate_all(., ~ round(., digits = 3))

pvalue <- pvalue %>%mutate(treatment_cat= "P-value")

balancetabraw <-  my_summarise(data, treatment_cat,  demslist)

my_summarise_noround <- function(data, group_var, summarise_var) {
  data %>%
    group_by(across({{ group_var }})) %>% 
    summarise(across({{ summarise_var }}, ~ paste(round(mean(.), digits = 3), " (", round(sd(.),digits = 2), ")", sep=""), .names = "{.col}")) #get mean and SD for each variable
}

nidata <- my_summarise_noround(data, treatment_cat, "region_Northern Ireland")

balancetab <- balancetabraw %>% mutate(`region_Northern Ireland`=nidata$`region_Northern Ireland`)

balancetabfinal <- balancetab %>%
pivot_longer(cols= !treatment_cat, names_to="variable",values_to="value") %>%
pivot_wider(names_from = treatment_cat, values_from =value) %>%
  mutate(variable = case_when(str_detect(variable,"member")~"Member",
                   str_detect(variable,"region_SE") ~"South East",
                   str_detect(variable,"fundraiser") ~"Fundraiser",
                   str_detect(variable,"volunteer")~"Volunteer",
                  str_detect(variable,"pret_donor1year")~"Donor",
                   str_detect(variable,"membership_amount_num") ~ "Membership amount",
                   str_detect(variable, "region") ~ str_replace(variable, "region_", "Region: "))) %>%
  mutate(variable = if_else(str_detect(variable, "Yorkshire"),"Region: Yorkshire", variable)) %>%
  rename("Variable"="variable") %>%
  relocate(Variable)

balancetabfinal





stargazer(balancetabfinal, summary = F, rownames = F, title="Balance on key pre-treatment covariates", out="output/balancetable.tex", notes = c("Mean and (SD) for key covariates. P-value from Pearson's chi-square test. "))




```

# SI: main regression tables

```{r controls}
# set covariates for regression models
controls <- c("dem_member", "dem_fundraiser", "dem_membership_amount_num", "dem_pret.volunteer", "`region_East Midlands`","`region_South West`", "`region_North West`", "`region_Wales`", "`region_Northern Ireland`",  "`region_missing`", "`region_Scotland`", "`region_London`","`region_South East`", "`region_Eastern`", "`region_Yorkshire and the Humber`", "`region_North East`", "`region_West Midlands`", "pret_donor1year", "treat_dblask")




```


```{r regfunction, echo=F}

uk.reg <- function(outcome){
lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treat_negative+treat_issue, data=data)  #main effects of negative cue and issue cue
lm1$term <- c( "Intercept","Negative cue", "Issue cue")


lm2 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treat_negative*treat_issue, data=data)  #interaction effects regression model
 lm2$term <- c("Intercept", "Negative cue", "Issue cue", "Negative*Issue") #set coefficient names for lm2





text_model3 <- paste("~ treat_negative+treat_issue +", paste(controls, collapse = " + ")) # model 1 with covariates
lm3 <- lm_robust ((eval(parse(text=paste(outcome, text_model3, sep="")))),  data=data)  


lm3$term <- c("Intercept","Negative cue", "Issue cue","cov_member","cov_fundraiser","cov_membership_amount_num","cov_pret.volunteer", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12","cov_region13", "cov_pret_donor1year", "cov_treat_dblask") #set coefficient names for lm3




text_model4 <- paste("~ treat_negative*treat_issue  +", paste(controls, collapse = " + ")) #interaction model with covariates

lm4 <- lm_robust ((eval(parse(text=paste(outcome, text_model4, sep="")))),  data=data)  


lm4$term <- c("Intercept","Negative cue", "Issue cue","cov_member","cov_fundraiser","cov_membership_amount_num","cov_pret.volunteer", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12","cov_region13", "cov_pret_donor1year", "cov_treat_dblask", "Negative*Issue") #set coefficient names for lm4


# output regression model in tex. format

regfile <- texreg(list(lm1, lm2, lm3, lm4), include.ci = FALSE, 
                    digits = 3,
                    stars = c(0.01,0.05,0.1),
                    custom.model.names=c("M1", "M2", "M3", "M4"),
                    caption= paste("Marginal effect of political identity cues on \n DV: ", names(outcome),  sep=""),
                  omit.coef = "(cov_)",
                  custom.gof.rows=(list("Covariates?" = c("No", "No", "Yes", "Yes"))),
                  custom.note = "\\item %stars. \\item Covariates include double ask, region, membership status and amount, past volunteering, fundraising and donations behaviour",
                 threeparttable = T,
                    label= paste("table:", outcome, "_reg", sep=""),
                 use.packages = FALSE, #this prevents the usepackage option from printing in the table in latex,
                 include.rmse = FALSE
                    )


print(regfile)
print(regfile, file =paste("output/si_", outcome, "_reg.tex", sep=""))
# save tex. output in file 

}



uk.reg.logit <- function(outcome){
lm1 <- glm(formula = (eval(parse(text=paste(outcome, sep="")))) ~ treat_negative+treat_issue , data=data, family = "binomial")  #main effects

attr(lm1$coefficients, "names") <- c( "Intercept","Negative cue", "Issue cue")





lm2 <- glm(formula = (eval(parse(text=paste(outcome, sep="")))) ~ treat_negative*treat_issue ,  data=data, family = "binomial")  #interaction effects regression model


 attr(lm2$coefficients, "names") <- c("Intercept", "Negative cue", "Issue cue", "Negative*Issue") #set coefficient names for lm2





text_model3 <- paste("~ treat_negative+treat_issue +", paste(controls, collapse = " + "))
lm3 <- glm(formula =(eval(parse(text=paste(outcome, text_model3, sep="")))),  data=data, family = "binomial")  


lm3$term <- c("Intercept","Negative cue", "Issue cue","cov_member", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12","cov_region13", "cov_fundraiser", "cov_pret.volunteer", "cov_membership_amount_num", "cov_pret_donor1year", "cov_treat_dblask") #set coefficient names for lm3




attr(lm3$coefficients, "names") <- c("Intercept","Negative cue", "Issue cue","cov_member", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12","cov_region13", "cov_fundraiser", "cov_pret.volunteer", "cov_membership_amount_num", "cov_pret_donor1year", "cov_treat_dblask") #set coefficient names for lm3




text_model4 <- paste("~ treat_negative*treat_issue  +", paste(controls, collapse = " + "))
lm4 <- glm(formula =(eval(parse(text=paste(outcome, text_model4, sep="")))),  data=data, family = "binomial")  


lm4$term <- c("Intercept","Negative cue", "Issue cue","cov_member", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12", "cov_fundraiser","cov_region13", "cov_pret.volunteer", "cov_membership_amount_num", "cov_pret_donor1year", "cov_treat_dblask", "Negative*Issue") #set coefficient names for lm4

attr(lm4$coefficients, "names") <- c("Intercept","Negative cue", "Issue cue","cov_member", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12", "cov_fundraiser","cov_region13", "cov_pret.volunteer", "cov_membership_amount_num", "cov_pret_donor1year", "cov_treat_dblask", "Negative*Issue") #set coefficient names for lm4






regfile <- texreg(list(lm1, lm2, lm3, lm4), include.ci = FALSE, 
                    digits = 3,
                    stars = c(0.01,0.05,0.1),
                    custom.model.names=c("M1", "M2", "M3", "M4"),
                    caption= paste("Logit model: Marginal effect of political identity cues on \n DV: ", names(outcome), ". Coefficients represent Log odds.", sep=""),
                  omit.coef = "(cov_)",
                  custom.gof.rows=(list("Covariates?" = c("No", "No", "Yes", "Yes"))),
                  custom.note = "\\item %stars. \\item Covariates include double ask, region, membership status and amount, past volunteering, fundraising and donations behaviour",
                 threeparttable = T,
                    label= paste("table:", outcome, "_logit_reg", sep=""),
                 use.packages = FALSE,#this prevents the usepackage option from printing in the table in latex
                 include.rmse = FALSE 
                    )


print(regfile)
print(regfile, file =paste("output/si_", outcome, "_logitreg.tex", sep=""))


}



```

# Tables A6 - A8

```{r applyregfunction, echo=F}


# Table A.3
uk.reg(donor)

# Table A.4
uk.reg(donation_nr)

# Table A.5
uk.reg(log_donation_amount_7days)



# Robustness checks

# Table A.6
uk.reg.logit(donor)

# Table A.7
uk.reg(log_donation_nr7days)

# Table A.8
uk.reg(donation_amount)



```


# SI: Tables A9 - A11, Regression models to compare outcomes a 2 days, 7 days and 14 days post-treatment

```{r differentpostt_periods, echo=F}

uk.reg.postperiods <- function(outcome1, outcome2, outcome3){
lm1 <- lm_robust((eval(parse(text=paste(outcome1, sep="")))) ~ treat_negative+treat_issue ,  data=data)  #main effects
lm1$term <- c( "Intercept","Negative cue", "Issue cue")
print(lm1)

lm2 <- lm_robust((eval(parse(text=paste(outcome1, sep="")))) ~ treat_negative*treat_issue ,  data=data)  #interaction effects regression model
 lm2$term <- c("Intercept", "Negative cue", "Issue cue", "Negative*Issue") #set coefficient names for lm2

 lm3 <- lm_robust((eval(parse(text=paste(outcome2, sep="")))) ~ treat_negative+treat_issue ,  data=data)  #main effects
lm3$term <- c( "Intercept","Negative cue", "Issue cue")
print(lm1)

lm4 <- lm_robust((eval(parse(text=paste(outcome2, sep="")))) ~ treat_negative*treat_issue ,  data=data)  #interaction effects regression model
 lm4$term <- c("Intercept", "Negative cue", "Issue cue", "Negative*Issue") #set coefficient names for lm2

 lm5 <- lm_robust((eval(parse(text=paste(outcome3, sep="")))) ~ treat_negative+treat_issue ,  data=data)  #main effects
lm5$term <- c( "Intercept","Negative cue", "Issue cue")
print(lm1)

lm6 <- lm_robust((eval(parse(text=paste(outcome3, sep="")))) ~ treat_negative*treat_issue ,  data=data)  #interaction effects regression model
 lm6$term <- c("Intercept", "Negative cue", "Issue cue", "Negative*Issue") #set coefficient names for lm2


regfile <- texreg(list(lm5,lm6,  lm3, lm4,lm1, lm2), include.ci = FALSE, 
                    digits = 3,
                    stars = c(0.01,0.05,0.1),
                    custom.model.names=c( "2 days", "2 days", "7 days", "7 days","14 days", "14 days"),
                    caption= paste("Marginal effect of political identity cues on \n DV: ", names(outcome3),  sep=""),
                  omit.coef = "(cov_)",
                  custom.gof.rows=(list("Covariates?" = c("No", "No", "No", "No", "No", "No"))),
                  custom.note = "\\item %stars. \\item The 14 days model shows the results 14 days post-treatment. \\item The 7 days model shows the results 7 days post-treatment. \\item The 2 days model shows the results 2 days post-treatment.",
                 threeparttable = T,
                    label= paste("table:", outcome2, "_reg_postperiod", sep=""),
                 use.packages = FALSE, #this prevents the usepackage option from printing in the table in latex
                 include.rmse = FALSE   )


print(regfile)
print(regfile, file =paste("output/", outcome2, "_reg_postperiod.tex", sep=""))


}

# Table A.9

uk.reg.postperiods(donor_14days, donor,donor_2days)

# Table A.10

uk.reg.postperiods(donation_nr_14days, donation_nr,donation_nr_2days)

# Table A.11

uk.reg.postperiods(log_donation_amount_14days, log_donation_amount_7days,log_donation_amount_2days)
``` 

# SI: Tables A12 -A14, double ask vs single ask 



```{r regfunction_dblask, echo=F}

uk.reg.dblask <- function(outcome){
  
lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treat_dblask ,  data=data)  #main effects
lm1$term <- c( "Mean(Only donation ask)","Donation and volunteering ask")
print(lm1)
  


text_model3 <- paste("~ treat_dblask +", paste(controls, collapse = " + "))
lm3 <- lm_robust ((eval(parse(text=paste(outcome, text_model3, sep="")))),  data=data)  


lm3$term <- c( "Mean(Only donation ask)","Donation and volunteering ask","cov_member", "cov_region_1","cov_region_2","cov_region_3","cov_region4","cov_region5","cov_region6","cov_region7","cov_region8","cov_region9","cov_region10","cov_region11","cov_region12", "cov_region13", "cov_fundraiser", "cov_pret.volunteer", "cov_membership_amount_num", "cov_pret_donor1year") #set coefficient names for lm3

print(summary(lm3))





regfile <- texreg(list(lm1, lm3), include.ci = FALSE, 
                    digits = 3,
                    stars = c(0.01,0.05,0.1),
                    custom.model.names=c("M1", "M2"),
                    caption= paste("Marginal effect of Double vs single ask on \n DV: ", names(outcome),  sep=""),
                  omit.coef = "(cov_)",
                  custom.gof.rows=(list("Covariates?" = c("No", "Yes"))),
                  custom.note = "\\item %stars. \\item Covariates include region, membership status and amount, past volunteering, fundraising and donations behaviour",
                 threeparttable = T,
                    label= paste("table:", outcome, "_dblask_reg", sep=""),
                 use.packages = FALSE, #this prevents the usepackage option from printing in the table in latex
                  include.rmse = FALSE  )


print(regfile)
print(regfile, file =paste("output/", outcome, "_dblask_reg.tex", sep=""))


}

# Table A.12
uk.reg.dblask(donor)

# Table A.13 
uk.reg.dblask(donation_nr)


# Table A.14
uk.reg.dblask(log_donation_amount_7days)

```







# SI Figure A.4: Negative vs Positive, Daily donations, Y: Pr(Donation)

```{r figa4_marginsfunc, echo=F}

margins.func <- function(outcome,n){
  lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treat_negative+treat_issue,  data=data)
  m <- margins(lm1) %>%
    summary %>%
    as.data.frame() %>%
    mutate(days=n)
  return(m)
}

allm <- data.frame()
for (i in 1:14) {
  outcome <- paste0("donor", i, "days")
  m <- margins.func(outcome, i)
  allm <- rbind(allm, m)
}

allm_pret <- data.frame()
for (i in 1:7) {
  outcome <- paste0("pret_donor", i, "days")
  m <- margins.func(outcome, i)
  allm_pret <- rbind(allm_pret, m)
}




allm_pret <- allm_pret %>% mutate(days= 0-days)

allm_combined <- rbind(allm, allm_pret)

allm_combined %>%
  filter(factor=="treat_negative") %>%
  mutate(colorgroup = case_when(days<0 ~ "light",
                                days>0  ~ "middle"))%>%
ggplot(., aes(days, AME, color=colorgroup)) +
  geom_point(size=2) +
  geom_errorbar(aes(ymax = lower, ymin = upper), size= 1,width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("\n Days since treatment") +
  ylab("") +
  theme_minimal() +
  labs(title="Marginal effect of negative vs positive political identity cue on Pr(donation)")+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
 scale_x_continuous(breaks=seq(-7,14,2))+
  geom_vline(xintercept = 0, linetype = "solid", colour="red")+
  scale_color_manual(values=c( "#969696", "#0b0a0a" ))



ggsave("output/si_figa4_overtime_itt_negative.png",width =24, height = 19, units = "cm", dpi=320)
```



# SI Figure A.5: Issue-Negative interaction, Daily donations, Y: Pr(Donation)


```{r figa5_marginsinteract, echo=F}


## interaction


margins.func <- function(outcome,n){
  
lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treat_negative*treat_issue,  data=data)

m<- margins(lm1, at = list(treat_issue=c(0,1))) %>%
  summary %>%
  as.data.frame() %>%
  mutate (days=n)


m <- m %>% mutate(partydesc = case_when (treat_issue==0 ~ "Party cue",
                                                             treat_issue ==1 ~ "Issue cue"))


return(m)
}


allm_interact <- data.frame()
for (i in 1:14) {
  outcome <- paste0("donor", i, "days")
  m <- margins.func(outcome, i)
  allm_interact <- rbind(allm_interact, m)
}

allm_interact_pret <- data.frame()
for (i in 1:7) {
  outcome <- paste0("pret_donor", i, "days")
  m <- margins.func(outcome, i)
  allm_interact_pret <- rbind(allm_interact_pret, m)
}
allm_interact_pret <- allm_interact_pret %>% mutate(days= 0-days)

allm_combined_interact <- rbind(allm_interact, allm_interact_pret)


allm_combined_interact %>%
  mutate(colorgroup = case_when(days<0 ~ "light",
                                days>0  ~ "middle"))%>%
  filter(factor=="treat_negative") %>%
ggplot(., aes(days, AME, color=colorgroup)) +
  geom_point(size=2) +
  geom_errorbar(aes(ymax = lower, ymin = upper), size= 1,width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("Days since treatment") +
  ylab("") +
  theme_minimal() +
  labs(title="Marginal effect of negative vs positive cue, conditional on issue cue, on Pr(donation)") +
  facet_wrap(~partydesc)+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
 scale_x_continuous(breaks=seq(-7,14,2))+
  geom_vline(xintercept = 0, linetype = "solid", colour="red")+
  scale_color_manual(values=c( "#969696", "#0b0a0a" ))



ggsave("output/si_figa5_overtime_itt_negative_interact.png",width =24, height = 19, units = "cm", dpi=320)
```


# SI Figure A.6: Treatment - Control, Daily donations, Y: Donation amount (logged)

```{r marginsfuncdonamount}
# for donation amount

scale2 <- function(x, na.rm = FALSE) log((x+1)) 
data <- data %>% mutate_at(vars(matches("donation_total")), scale2)


margins.func <- function(outcome,n){
  
lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treatment_cat,  data=data)

m<- margins(lm1) %>%
  summary %>%
  as.data.frame() %>%
  mutate (days=n)



return(m)
}


allm_control <- data.frame()
for (i in 1:14) {
  outcome <- paste0("donation_total", i, "days")
  m <- margins.func(outcome, i)
  allm_control <- rbind(allm_control, m)
}

allm_control_pret <- data.frame()
for (i in 1:7) {
  outcome <- paste0("pret_donation_total", i, "days")
  m <- margins.func(outcome, i)
  allm_control_pret <- rbind(allm_control_pret, m)
}
allm_control_pret <- allm_control_pret %>% mutate(days= 0-days)

allm_combined_control <- rbind(allm_control, allm_control_pret)



allm_combined_control <- allm_combined_control %>% mutate (factorname = str_replace(factor, "treatment_cat", ""))


allm_combined_control %>%
  mutate(colorgroup = case_when(days<0 ~ "light",
                                days>0  ~ "middle"))%>%
ggplot(., aes(days, AME, color=colorgroup))+
  geom_point(size=2) +
  geom_errorbar(aes(ymax = lower, ymin = upper), size= 1,width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("Days since treatment") +
  ylab("") +
  theme_minimal() +
  labs(title="Marginal effect of political identity cues (vs Control) on logged Donation amount") +
  facet_wrap(~factorname)+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
 scale_x_continuous(breaks=seq(-7,14,2))+
  geom_vline(xintercept = 0, linetype = "solid", colour="red")+
  scale_color_manual(values=c( "#969696", "#0b0a0a" ))





ggsave("output/si_figa6_overtime_itt_tvscontrol_loggeddonamount.png",width =24, height = 19, units = "cm", dpi=320)


                        
```


# SI, Figure A.7: CITT graph by donor type, DV: Pr(donation)


```{r figa7_cittbydonor, echo=F}




lm1 <- lm_robust(donor14days ~ treatment_cat*donor.type ,  data=data)


lm1_margins_14days <- margins(lm1,at = list(donor.type= c("small", "large", "none"))) %>%
  summary %>%
  as.data.frame() 



lm1_margins_14days <- lm1_margins_14days %>% mutate(days="14 days")

lm1_margins_14days <- lm1_margins_14days %>% filter(!grepl("donor.type",factor))


lm1_margins_14days <- lm1_margins_14days  %>% unique()

lm1 <- lm_robust(donor7days  ~ treatment_cat*donor.type ,  data=data)



lm1_margins_7days <- margins(lm1,at = list(donor.type= c("small", "large", "none"))) %>%
  summary %>%
  as.data.frame() 

lm1_margins_7days <- lm1_margins_7days %>% mutate(days="7 days")

lm1_margins_7days <- lm1_margins_7days %>% filter(!grepl("donor.type",factor))


lm1_margins_7days <- lm1_margins_7days  %>% unique()


lm1_all <- rbind(lm1_margins_14days, lm1_margins_7days)

lm1_all <- lm1_all %>%
  mutate (donordesc= case_when(donor.type=="none" ~ "Not donor (0 GBP)",
                               donor.type=="small" ~ "Small donor (1-25 GBP)",
                               donor.type=="large" ~ "Large donor (>25 GBP)"),
          donordesc= factor(donordesc, levels= c("Not donor (0 GBP)","Small donor (1-25 GBP)", "Large donor (>25 GBP)" )),
          daysf = factor(days, levels = c("7 days", "14 days")))
# effect of negative vs positive for issue (low, med and high donors) and for non issue - low, mid and high donors


lm1_all <- lm1_all %>% mutate(partydesc = str_replace(factor, "treatment_cat", ""))
 #The palette with grey:
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# The palette with black:
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


  
lm1_all %>%
ggplot(., aes(donordesc, AME,group=daysf, color=daysf)) +
  geom_point(size=2,position = position_dodge2(width = 0.2)) +
  geom_linerange(aes(ymax = lower, ymin = upper), size= 1, position = position_dodge2(width=0.2)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("Pre-treatment donor type") +
  ylab("CITT of treatment cue (vs Control) on probability of donating") +
  theme_minimal() +
  labs(title="Conditional ITT of different treatment cues (vs Control) on Pr(donation)", color="Days since treatment") +
  scale_colour_manual(values=cbPalette) +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  facet_wrap(~partydesc)

ggsave("output/si_figa7_citt_donortype_donationpr_forallcues.png",width =25, height = 19, units = "cm", dpi=320)


```

# SI, Figure A.8: CITT graph by donor type, DV: donation amount (logged)


```{r figa8_cittbydonoramount, echo=F}


lm1 <- lm_robust(log_donation_total14days ~ treatment_cat*donor.type ,  data=data)


lm1_margins_14days <- margins(lm1,at = list(donor.type= c("small", "large", "none"))) %>%
  summary %>%
  as.data.frame() 

lm1_margins_14days <- margins(lm1,at = list(donor.type= c("small", "large", "none"), treatment_cat=c("Control", "Positive-Issue","Positive-Party", "Negative-Issue", "Negative-Party"))) %>%
  summary %>%
  as.data.frame() 

lm1_margins_14days <- lm1_margins_14days %>% mutate(days="14 days")

lm1_margins_14days <- lm1_margins_14days %>% filter(!grepl("donor.type",factor))


lm1_margins_14days <- lm1_margins_14days  %>% unique()

lm1_margins_14days <- lm1_margins_14days %>% dplyr::select(-treatment_cat) %>% unique()

lm1 <- lm_robust(log_donation_total7days  ~ treatment_cat*donor.type ,  data=data)



lm1_margins_7days <- margins(lm1,at = list(donor.type= c("small", "large", "none"))) %>%
  summary %>%
  as.data.frame() 

lm1_margins_7days <- lm1_margins_7days %>% mutate(days="7 days")

lm1_margins_7days <- lm1_margins_7days %>% filter(!grepl("donor.type",factor))




lm1_all <- rbind(lm1_margins_14days, lm1_margins_7days)

lm1_all <- lm1_all %>%
  mutate (donordesc= case_when(donor.type=="none" ~ "Not donor (0 GBP)",
                               donor.type=="small" ~ "Small donor (1-25 GBP)",
                               donor.type=="large" ~ "Large donor (>25 GBP)"),
          donordesc= factor(donordesc, levels= c("Not donor (0 GBP)","Small donor (1-25 GBP)", "Large donor (>25 GBP)" )),
          daysf = factor(days, levels = c("7 days", "14 days")))



lm1_all <- lm1_all %>% mutate(partydesc = str_replace(factor, "treatment_cat", ""))



  
lm1_all %>%
ggplot(., aes(donordesc, AME,group=daysf, color=daysf)) +
  geom_point(size=2,position = position_dodge2(width = 0.2)) +
  geom_linerange(aes(ymax = lower, ymin = upper), size= 1, position = position_dodge2(width=0.2)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("Pre-treatment donor type") +
  ylab("CITT of treatment cue (vs Control) on logged donation amount") +
  theme_minimal() +
  labs(title="Conditional ITT of different treatment cues (vs Control) on donation amount", color="Days since treatment") +
  scale_colour_manual(values=cbPalette) +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  facet_wrap(~partydesc)

ggsave("output/si_figa8_citt_donortype_donationamount_forallcues.png",width =25, height = 19, units = "cm", dpi=320)


```